package cn.fl.relationmap.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.excel.PrjContBatchMakeExcel;
import cn.fl.project.vo.PrjContCreatRecVO;
import cn.fl.relationmap.excel.ExcelVOToDTO;
import cn.fl.relationmap.excel.RmEmployeeExcel;
import cn.fl.relationmap.excel.RmEmployeeExcelImportParam;
import cn.fl.relationmap.service.IRmEmployeeBscInfoService;
import cn.fl.relationmap.vo.EmployeeListRespVO;
import cn.lb.framework.poi.excel.ExcelImportUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import tc.relationmap.facade.relationmap.employee.api.IRmEmployeeBscInfoInsertOrUpdateApiService;
import tc.relationmap.facade.relationmap.employee.api.IRmEmployeeBscInfoSelectApiService;
import tc.relationmap.facade.relationmap.employee.dto.RmEmployeeBscInfoDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;

import javax.annotation.Resource;


/**
 * @Description: 员工基本信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class RmEmployeeBscInfoServiceImpl implements IRmEmployeeBscInfoService {

	@Resource
	private IRmEmployeeBscInfoSelectApiService rmEmployeeBscInfoSelectApiService;
	@Resource
	private IRmEmployeeBscInfoInsertOrUpdateApiService rmEmployeeBscInfoInsertOrUpdateApiService;
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(RmEmployeeBscInfoServiceImpl.class);

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = rmEmployeeBscInfoSelectApiService.selectByPager(page);
		List<RmEmployeeBscInfoDTO> resultList = (List<RmEmployeeBscInfoDTO>) page.getResultList();
		List<EmployeeListRespVO> listRespVOS = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(resultList)){
			for (RmEmployeeBscInfoDTO rmEmployeeBscInfoDTO : resultList) {
				EmployeeListRespVO employeeListRespVO = new EmployeeListRespVO(rmEmployeeBscInfoDTO.getEmployeeNo(),rmEmployeeBscInfoDTO.getEmployeeName());
				listRespVOS.add(employeeListRespVO);
			}
		}
		pager.setResultList(listRespVOS);
		return pager;
	}

	@Override
	public void addBatchByUpload(MultipartHttpServletRequest multipartRequest) {
		BaseLogUtils.info(logger, "addBatchByUpload", "数据上传开始", CurrentThreadContext.getCurrentUserName());
		try {

			Iterator<String> iter = multipartRequest.getFileNames();
			// 如果没有文件 抛出异常
			if (!iter.hasNext()) {
				BaseLogUtils.info(logger, "fileUpload", "文件上传失败,未选中文件内容.", CurrentThreadContext.getCurrentUserName());
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请选择要上传的文件!");
			}
			//只取第一个文件
			MultipartFile f = multipartRequest.getFile(iter.next());

			//限制大小为100k
			if(f.getSize()>10000000){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请上传小于10000kb的文件!");
			}
			String fileName = f.getOriginalFilename().toLowerCase();
			if(!fileName.endsWith(".xlsx")  && !fileName.endsWith(".xls")) {
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "文件格式不对，请选择xls或xlsx格式！!");
			}

			if(f.getSize()==0){
				BaseLogUtils.info(logger, "fileUpload", "文件上传失败,未选中文件内容.", CurrentThreadContext.getCurrentUserName());
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请选择要上传的文件!");
			}
			List<RmEmployeeExcel> rmEmployeeExcels = ExcelImportUtil.importExcel(f.getInputStream(), RmEmployeeExcel.class, RmEmployeeExcelImportParam.buildImportParam());
			List<RmEmployeeBscInfoDTO> rmEmployeeBscInfoDTOS = Lists.newArrayList();

			for (RmEmployeeExcel rmEmployeeExcel : rmEmployeeExcels) {
				//处理添加信息
				rmEmployeeBscInfoDTOS.add(ExcelVOToDTO.toDTO(rmEmployeeExcel));
			}
			if(CollectionUtils.isEmpty(rmEmployeeBscInfoDTOS)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "上传可用数据为空");
			}
			rmEmployeeBscInfoInsertOrUpdateApiService.batchInsertEmploy(rmEmployeeBscInfoDTOS);


			BaseLogUtils.info(logger, "upload", "合同生成文件上传结束", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			BaseLogUtils.info(logger, "fileUpload", "文件上传失败.ERROR", CurrentThreadContext.getCurrentUserName(), ex);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, ex.getMessage());
		} catch (Exception ex) {
			BaseLogUtils.error(logger, "fileUpload", "文件上传失败,系统错误.ERROR", CurrentThreadContext.getCurrentUserName(), ex);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统异常!");
		}
	}
}
